package com.google.code.ptrends.Application.DAL;

import java.io.Serializable;
import java.util.List;

import javax.ejb.LocalBean;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;

import com.google.code.ptrends.Application.DAL.entities.ItemEntity;

@Stateless
@LocalBean
public class RepositoryServiceBean implements Serializable {

	private static final long serialVersionUID = 1L;

	@PersistenceContext(name = "Application.DAL")
	private transient EntityManager entityManager;

	public RepositoryServiceBean() {
	}

	public List<String> getAllSuppliers() {
		TypedQuery<String> query = entityManager.createQuery(
				"SELECT s.name FROM SupplierEntity s", String.class);
		return query.getResultList();
	}

	public List<String> getAllCategories() {
		TypedQuery<String> query = entityManager.createQuery(
				"SELECT c.name FROM CategoryEntity c", String.class);
		return query.getResultList();
	}

	public List<String> getAllManufacturers(String category) {
		String withCategory = "SELECT DISTINCT i.manufacturer.name FROM ItemEntity i WHERE i.category.name = '"
				+ category + "'";
		String withoutCategory = "SELECT m.name FROM ManufacturerEntity m";
		TypedQuery<String> query;
		if (category != null && !"".equals(category))
			query = entityManager.createQuery(withCategory, String.class);

		else
			query = entityManager.createQuery(withoutCategory, String.class);
		return query.getResultList();
	}

	public List<String> getAllClasses() {
		TypedQuery<String> query = entityManager.createQuery(
				"SELECT c.name FROM ClassesEntity c", String.class);
		return query.getResultList();
	}

	private String createParametersStr(String supplier, String category,
			String manufacturer, String classes) {
		StringBuilder sb = new StringBuilder();
		if (supplier != null && !"".equals(supplier)) {
			sb.append("i.supplier.name = ");
			sb.append("'" + supplier + "'");
		}
		if (category != null && !"".equals(category)) {
			if (sb.length() != 0) {
				sb.append(" AND ");
			}
			sb.append("i.category.name = ");
			sb.append("'" + category + "'");
		}
		if (manufacturer != null && !"".equals(manufacturer)) {
			if (sb.length() != 0) {
				sb.append(" AND ");
			}
			sb.append("i.manufacturer.name = ");
			sb.append("'" + manufacturer + "'");
		}
		if (classes != null && !"".equals(classes)) {
			if (sb.length() != 0) {
				sb.append(" AND ");
			}
			sb.append("i.classes.name = ");
			sb.append("'" + classes + "'");
		}
		if (sb.length() != 0)
			return " WHERE " + sb.toString();
		return sb.toString();
	}

	public List<ItemEntity> getItemsByParameters(String supplier,
			String category, String manufacturer, String classes) {
		StringBuilder sb = new StringBuilder();
		sb.append("SELECT DISTINCT i FROM ItemEntity i ");
		sb.append(createParametersStr(supplier, category, manufacturer, classes));
		TypedQuery<ItemEntity> query = entityManager.createQuery(sb.toString(),
				ItemEntity.class);
		return query.getResultList();
	}

}
